package com.young.ums.service.impl;

import com.young.common.exception.BaseException;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.annotation.Log;
import com.young.ums.model.SsoSession;
import com.young.ums.service.ISsoSessionService;
import com.young.ums.util.UmsConstants;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
* 登录认证会话信息服务实现
* Created by imrookie on 2018-9-15.
*/
@Service("ssoSessionService")
public class SsoSessionServiceImpl implements ISsoSessionService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(SsoSessionServiceImpl.class);

    private static final String MAPPER = "com.young.ums.mapper.ssoSession";//mapper的namespace

    //获取数据列表
    @Override
    public List<SsoSession> getList(SsoSession obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<SsoSession> getPage(SsoSession obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public SsoSession get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    @Override
    public SsoSession getByToken(String token) {
        return dataAccessService.getObject(MAPPER + ".getByToken", token);
    }

    //统计数量
    @Override
    public int count(SsoSession obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(SsoSession obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(SsoSession obj) {
        obj.setId(CommonUtil.getUUID());
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<SsoSession> list) {
        int i = 0;
        for(SsoSession item : list){
            i += this.insert(item);
        }
        return i;
    }

    /**
     * 下线指定会话
     *
     * @param sessionId
     * @return
     */
    @Override
    @Log("下线指定会话")
    public boolean offLine(String sessionId) {
        if (StringUtils.isBlank(sessionId)) {
            throw new BaseException("会话无效");
        }
        SsoSession ssoSession = new SsoSession();
        ssoSession.setId(sessionId);
        ssoSession.setStatus(UmsConstants.SsoSessionStatus.NO);
        if (this.update(ssoSession) == 1) {
            return true;
        }
        logger.error("[下线指定会话] 下线失败,会话编号为:{}", sessionId);
        throw new BaseException("下线失败");
    }

    /**
     * 根据token下线
     *
     * @param token
     * @return
     */
    @Override
    @Log("根据token下线会话")
    public int offLineByToken(String token) {
        if (StringUtils.isBlank(token)) {
            throw new BaseException("token无效");
        }
        SsoSession ssoSession = new SsoSession();
        ssoSession.setToken(token);
        ssoSession.setStatus(UmsConstants.SsoSessionStatus.NO);
        return dataAccessService.update(MAPPER + ".updateStatusByToken", ssoSession);
    }

    /**
     * 上线指定会话
     *
     * @param sessionId
     * @return
     */
    @Override
    @Log("上线指定会话")
    public boolean onLine(String sessionId) {
        if (StringUtils.isBlank(sessionId)) {
            throw new BaseException("会话无效");
        }
        SsoSession ssoSession = new SsoSession();
        ssoSession.setId(sessionId);
        ssoSession.setStatus(UmsConstants.SsoSessionStatus.YES);
        if (this.update(ssoSession) == 1) {
            return true;
        }
        logger.error("[上线指定会话] 上线失败,会话编号为:{}", sessionId);
        throw new BaseException("上线失败");
    }

}